﻿using HSGD.IRepository.BASE;
using HSGD.IServices.HSGD_Staff;
using HSGD.Services.BASE;
using HSGD.Model.Models.HSGD_Staff;
using HSGD.Model.Dtos.HSGD_Staff;
using AutoMapper;
using HSGD.Common.Attributes;
using HSGD.Common.DB;
using HSGD.IServices.HSGD_Logs;

namespace HSGD.Services.HSGD_Staff;
public class PersonServices : BaseServices<Person>, IPersonServices {
    //private readonly IBaseRepository<Person> _dal;
    //private readonly IRefEmailNoticeServices _refEmailNoticeServices;
    //public IRefEmailNoticeServices _refEmailNoticeServices { get; set; }
    //public IBaseServices<RefEmailNotice> _refEmailNoticeServices { get; set; }
    public IRefsServices refsServices { get; set; }
    public IPersonEducationInfoServices personEducationInfoServices { get; set; }
    public IHSGDLogsServices _logsServices { get; set; }
    IMapper _mapper;

    public PersonServices(IBaseRepository<Person> dal, /*IRefEmailNoticeServices refEmailNoticeServices,*/ IMapper mapper) {
        //_refEmailNoticeServices = refEmailNoticeServices;
        //this._dal = dal;
        base.BaseDal = dal;
        _mapper = mapper;
    }

    [UseTran(Propagation = Propagation.Required)]
    public async Task<PersonDto> AddPersonAsync(Person person) {
        var email = (await refsServices.GetRefEmailNotice()).FirstOrDefault(x => x.type == "CareerTrackGuideSubmit");
        var fiscalYear = (await refsServices.GetRefFiscalYear()).FirstOrDefault(x => x.FISCAL_YEAR == 2024);

        //var first = (await _refEmailNoticeServices.Query()).FirstOrDefault(x => x.type == "CareerTrackGuideSubmit");
        await Console.Out.WriteLineAsync($"{email.subject}->{email.message}");
        await Console.Out.WriteLineAsync($"{fiscalYear.Start_date}->{fiscalYear.End_Date}");

        //if (await Db.Queryable<Person>().AnyAsync(x => x.Person_System_ID == person.Person_System_ID)) {
        //    Db.Deleteable<Person>(new Person() { Person_System_ID = person.Person_System_ID }).ExecuteCommand();
        //    //throw new Exception($"Exist {person.Person_System_ID}");
        //}
        //var data = await Db.Insertable(person).ExecuteReturnEntityAsync();
        ////_dal.Query()
        //return _mapper.Map<PersonDto>(data);
        await _logsServices.AddAsync(new Model.Models.HSGD_Logs.Logs() { age = 12, name = $"name1", gender = 'M', create_dt = DateTime.Now });

        var db_person = await Db.Queryable<Person>().FirstAsync(x => x.Person_System_ID == person.Person_System_ID);
        if (db_person != null) {
            db_person.Update_Date = DateTime.Now;
            var _update = _mapper.Map<Person>(db_person);
            await Db.Updateable(_update).Where(x => x.Person_System_ID == person.Person_System_ID).ExecuteCommandAsync();
            return null;
        }
        else {
            var data = await Db.Insertable(person).ExecuteReturnEntityAsync();
            await personEducationInfoServices.AddAsync(new PersonEducationInfo() {
                Person_System_ID = data.Person_System_ID,
                School = "University of North Texas University of North Texas University of North Texas University of North Texas",
                Major = "Interdisciplinary Studies",
                Degree = "BA/BS",
                Year = "10/16/2023",
                gpa = "10.00",
                Expired_Date = DateTime.Now,
                Scan_Id = "4C319994-4D71-4AD2-B072-EC57B77CAC92",
            });
            return _mapper.Map<PersonDto>(data);
        }
    }

    public async Task<PersonDto> GetPersonByEinfoId(string eID) {
        var d = Db.Queryable<Person>().First(x => x.EInfo_id == eID);
        //var person = (await Query(a => a.EInfo_id == eID)).FirstOrDefault();
        var person = (await Query(a => a.EInfo_id == eID)).FirstOrDefault();
        var result = _mapper.Map<PersonDto>(person);
        var result1 = _mapper.Map<PersonDto>(d);
        return result;
    }

}